﻿using System.Web;
using System.Web.Security;
using good1.ApplicationServices.Interface;

namespace good1.Web.Controllers.Infrastructure.Session
{
    /// <summary>
    /// Only going to store data in session that is too sensitive for cookies or that cannot be stored there.
    /// Not even going to introduce it to controllers until there is a concrete need for it.
    /// </summary>
    public class HttpContextSessionContainer : ISessionContainer
    {
        private readonly HttpContextBase httpContext;
        
        public HttpContextSessionContainer()
        {
            httpContext = HttpContextFactory.GetHttpContext();
        }

        public bool Exists(string key)
        {
            object value = httpContext.Session[key];
            return value == null ? false : true;
        }

        public string GetValue(string key)
        {
            return httpContext.Session[key].ToString();
        }

        public T GetValue<T>(string key)
        {
            object value = httpContext.Session[key];
            return value == null ? default(T) : (T)value;
        }

        public void SetValue(string key, object value)
        {
            httpContext.Session[key] = value;
        }

        public void LogOut()
        {
            FormsAuthentication.SignOut();
        }
    }
}
